//
//  FQDCommentInputBar.m
//  FakeQDaily
//
//  Created by mewxiv on 9/4/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDCommentInputBar.h"

#import "Masonry.h"

@interface FQDCommentInputBar () <UITextViewDelegate>

// 评论输入框
@property (nonatomic, strong, readwrite) UITextView *commentTextView;
// 分割线
@property (nonatomic, strong) UIView *separateLine;
// 占位提示文本
@property (nonatomic, strong) UILabel *placeHolderLabel;
// 发送按钮
@property (nonatomic, strong) UIButton *sendButton;

@end

@implementation FQDCommentInputBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    _separateLine = [[UIView alloc] init];
    _separateLine.backgroundColor = FQDCommentInputBarSeparateLineColor;
    [self addSubview:_separateLine];
    
    _commentTextView = [[UITextView alloc] init];
    _commentTextView.font = FQDRegularFontWithSize(14.0f);
    _commentTextView.delegate = self;
    [self addSubview:_commentTextView];
    
    _placeHolderLabel = [[UILabel alloc] init];
    _placeHolderLabel.font = FQDRegularFontWithSize(14.0f);
    _placeHolderLabel.textColor = [UIColor lightGrayColor];
    _placeHolderLabel.text = @"添加评论...";
    [self addSubview:_placeHolderLabel];
    
    _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendButton.backgroundColor = FQDHighlightColor;
    _sendButton.layer.cornerRadius = 16.0f;
    _sendButton.hidden = YES;
    [_sendButton addTarget:self action:@selector(sendButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sendButton];
    
    [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
    _sendButton.titleLabel.font = FQDRegularFontWithSize(14.0f);
    
    WeakSelf;
    [_separateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(weakSelf);
        make.height.equalTo(@0.5);
    }];
    
    [_sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf).offset(-15);
        make.bottom.equalTo(weakSelf).offset(-6);
        make.size.mas_equalTo(CGSizeMake(65.0f, 32.0f));
    }];
    
    [_commentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(6.5);
        make.left.equalTo(weakSelf).offset(10);
        make.bottom.equalTo(weakSelf).offset(-5.5);
        make.right.equalTo(weakSelf.sendButton.mas_left).offset(-5);
        
        make.height.equalTo(@33);
    }];
    
    // 获取光标初始位置，占位文本在光标右边出现
    CGRect caretRect = [self.commentTextView caretRectForPosition:self.commentTextView.beginningOfDocument];
    [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.commentTextView).offset(caretRect.origin.x);
        make.right.equalTo(weakSelf.commentTextView);
        make.centerY.equalTo(weakSelf.commentTextView);
    }];
}

- (void)updateTextViewSize
{
    UITextView *textView = self.commentTextView;
    CGSize fitSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, MAXFLOAT)];
    CGFloat maxHeight = MIN(MAX(fitSize.height, 33.0f), 83.0f);
    if (maxHeight != textView.frame.size.height) {
        [textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(maxHeight));
        }];
    }
}

- (void)sendButtonTapped:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(inputBar:didClickedSendButton:)]) {
        [self.delegate inputBar:self didClickedSendButton:sender];
    }
    [self.commentTextView resignFirstResponder];
}

- (void)setPlaceHolderText:(NSString *)text
{
    self.placeHolderLabel.text = text;
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (![self.delegate inputBarShouldBeginEditing:self]) {
        return NO;
    }
    self.placeHolderLabel.hidden = ([textView.text length] > 0);
    self.sendButton.hidden = NO;
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    self.placeHolderLabel.hidden = ([textView.text length] > 0);
    [self updateTextViewSize];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    textView.text = @"";
    self.placeHolderLabel.hidden = ([textView.text length] > 0);
    self.sendButton.hidden = YES;
    [self updateTextViewSize];
}

@end
